<?php
namespace app\work\controller;

use \think\Controller;
use \think\Db;
class Notice extends Base
{
    /**
     * 排行榜:topTen
     * type(1是今天  2是所有)
     */
    public function topTen(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $today = date('Y-m-d');
        $where = array();
        $where['o_status'] = 2;
        if($type == 1){
            $where['o_date'] = $today;
        }
        $field = 'user_mynum';
        $field .= ',SUM(o_money) as money';
        $data = Db::table('up_work_order')->alias('o')
            ->join('up_work_user u','o.o_user_id=u.user_id','LEFT')
            ->where($where)
            ->field($field)
            ->group('o_user_id')
            ->order('money DESC')
            ->page(1,10)
            ->select();
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /**
     * 消息通知列表:notice放在shop里了，通用了
     * type $page
     */
//    public function notice(){
//        extract(input());
//        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
//        if($auth == false){
//            $returnData['status'] = 301;
//            $returnData['msg'] = "Token失效！请重新登录！";
//            return json_encode($returnData);
//        }
//        $time = Db::table('up_work_user')
//            ->where('user_id',$user_id)->value('user_time');
//        if($type == 1){
//            $data = Db::table('up_notice')
//                ->where('n_type',1)
//                ->where('n_delete','not like','%,'.$user_id.',%')
//                ->where('n_time','>=',$time)
//                ->order('n_time DESC')
//                ->page($page,15)
//                ->select();
//            if($data){
//                foreach ($data as $k=>$v){
//                    $data[$k]['read'] = 0;
//                    if(strpos($v['n_read'],",$user_id,") !== false){
//                        $data[$k]['read'] = 1;
//                    }
//                }
//            }
//        }else{
//            $data = Db::table('up_notice')
//                ->where('n_type',2)
//                ->where('n_user_id',$user_id)
//                ->where('n_time','>=',$time)
//                ->order('n_time DESC')
//                ->page($page,15)
//                ->select();
//        }
//        return json_encode(['status'=>200,'msg'=>$data]);
//    }

    /**
     * 消息通知详情
     * n_id
     */
//    public function noticeInfo(){
//        extract(input());
//        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
//        if($auth == false){
//            $returnData['status'] = 301;
//            $returnData['msg'] = "Token失效！请重新登录！";
//            return json_encode($returnData);
//        }
//        $notice = Db::table('up_notice')->where('n_id',$n_id)->find();
//        $type = $notice['n_type'];
//        if($type == 1){
//            $read = $notice['n_read'];
//            if(strpos($read,",$user_id,") !== false){
//                $read = 1;
//            }else{
//                $read = $read . $user_id . ',';
//                Db::table('up_notice')->where('n_id',$n_id)
//                    ->update(['n_read'=>$read]);
//            }
//        }else{
//            Db::table('up_notice')->where('n_id',$n_id)
//                ->update(['n_read'=>1]);
//        }
//        return json_encode(['status'=>200,'msg'=>$notice]);
//    }
    /**
     * 删除某个消息通知:delNotice
     * n_id
     */
//    public function delNotice(){
//        extract(input());
//        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
//        if($auth == false){
//            $returnData['status'] = 301;
//            $returnData['msg'] = "Token失效！请重新登录！";
//            return json_encode($returnData);
//        }
//        $notice = Db::table('up_notice')->where('n_id',$n_id)->find();
//        $type = $notice['n_type'];
//        if($type == 1){
//            $del = $notice['n_delete'];
//            $del = $del . $user_id . ',';
//            $res = Db::table('up_notice')->where('n_id',$n_id)
//                ->update(['n_delete'=>$del]);
//        }else{
//            $res = Db::table('up_notice')->where('n_id',$n_id)
//                ->delete();
//        }
//
//        if($res){
//            return json_encode(['status'=>200,'msg'=>'成功']);
//        }else{
//            return json_encode(['status'=>200,'msg'=>'成功']);
//        }
//    }
    /**
     * 意见反馈：feedback
     * $content,$phone,$type
     */
    public function feedback(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = array();
        $data['f_user_id'] = $user_id;
//        $data['f_type'] = $type;
        $data['f_content'] = $content;
//        $data['f_phone'] = $phone;
        $data['f_time'] = date('Y-m-d H:i:s');
        $data['f_where'] = 2;
        $res = Db::table('up_feedback')->insert($data);
        if($res){
            return json_encode(['status'=>200,'msg'=>'提交成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'提交失败']);
        }
    }
}